﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.BCThoigianlamviecthang;

namespace ERP_RORZE_TOOL.Tool.CheckThoiGianLamViec
{
    public partial class ucCheckWorkTime : DevExpress.XtraEditors.XtraUserControl
    {
        DXPopupMenu menu;

        public ucCheckWorkTime()
        {
            InitializeComponent();
            InitPopupMenu();
            BindDataReportList();
            this.dropbtExport.DropDownControl = menu;
            this.lblFormName.Text = ReportConstants.FormName;
        }

        private void cmbReportList_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ReportConstants.Item item = (ReportConstants.Item)cmbReportList.SelectedItem;
            if (item.Value != -1)
            {
                BindDataToGridview(item.Value);
            }
        }

        private void BindDataReportList()
        {
            cmbReportList.Items.Add(new ReportConstants.Item("Select..."));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QTongthoigianCacBophan, ReportConstants.ReportId.QTongthoigianCacBophan));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QTongthoigiannghiCacBophan, ReportConstants.ReportId.QTongthoigiannghiCacBophan));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QtongOvertimeCacbophan, ReportConstants.ReportId.QtongOvertimeCacbophan));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QmacongviecInspection, ReportConstants.ReportId.QmacongviecInspection));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QmacongviecMain, ReportConstants.ReportId.QmacongviecMain));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QmacongviecOffice, ReportConstants.ReportId.QmacongviecOffice));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QmacongviecPlanning, ReportConstants.ReportId.QmacongviecPlanning));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QmacongviecSurface, ReportConstants.ReportId.QmacongviecSurface));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.QmacongviecTech, ReportConstants.ReportId.QmacongviecTech));
            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.Qmacongviec_Via, ReportConstants.ReportId.Qmacongviec_Via));

            cmbReportList.SelectedItem = cmbReportList.Items[0];
        }

        private void BindDataToGridview(int reportId)
        {
            DataTable dt = new DataTable();
            switch (reportId)
            {
                case ReportConstants.ReportId.QTongthoigianCacBophan:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QTongthoigianCacBophan);
                    gridView1.ViewCaption = ReportConstants.ReportName.QTongthoigianCacBophan;
                    break;
                case ReportConstants.ReportId.QTongthoigiannghiCacBophan:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QTongthoigiannghiCacBophan);
                    gridView1.ViewCaption = ReportConstants.ReportName.QTongthoigiannghiCacBophan;
                    break;
                case ReportConstants.ReportId.QtongOvertimeCacbophan:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QtongOvertimeCacbophan);
                    gridView1.ViewCaption = ReportConstants.ReportName.QtongOvertimeCacbophan;
                    break;
                case ReportConstants.ReportId.QmacongviecInspection:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QmacongviecInspection);
                    gridView1.ViewCaption = ReportConstants.ReportName.QmacongviecInspection;
                    break;
                case ReportConstants.ReportId.QmacongviecMain:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QmacongviecMain);
                    gridView1.ViewCaption = ReportConstants.ReportName.QmacongviecMain;
                    break;
                case ReportConstants.ReportId.QmacongviecOffice:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QmacongviecOffice);
                    gridView1.ViewCaption = ReportConstants.ReportName.QmacongviecOffice;
                    break;
                case ReportConstants.ReportId.QmacongviecPlanning:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QmacongviecPlanning);
                    gridView1.ViewCaption = ReportConstants.ReportName.QmacongviecPlanning;
                    break;
                case ReportConstants.ReportId.QmacongviecSurface:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QmacongviecSurface);
                    gridView1.ViewCaption = ReportConstants.ReportName.QmacongviecSurface;
                    break;
                case ReportConstants.ReportId.QmacongviecTech:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QmacongviecTech);
                    gridView1.ViewCaption = ReportConstants.ReportName.QmacongviecTech;
                    break;
                case ReportConstants.ReportId.Qmacongviec_Via:
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.Qmacongviec_Via);
                    gridView1.ViewCaption = ReportConstants.ReportName.Qmacongviec_Via;
                    break;
                
                default:
                    break;
            }

            gridControl1.DataSource = dt;
            gridView1.PopulateColumns();
            gridView1.BestFitColumns();
        }

        //Export to excel button

        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount > 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }
    }
}
